import Cookies from 'js-cookie'
import { getStore, setStore, removeStore } from '@/utils/store'
import { encryption } from '@/utils/index'
export const loginAuthorization = 'Y2M6Y2M='

const TokenKey = 'Admin-Token'

export function getToken() {
  return Cookies.get(TokenKey)
}

export function setToken(token) {
  return Cookies.set(TokenKey, token)
}

export function removeToken() {
  removeAccount()
  return Cookies.remove(TokenKey)
}

// 保存登录名
const AccountKey = 'flag1'

export function getAccount() {
  /* const flagStr = Cookies.get(AccountKey)
  return flagStr ? JSON.parse(flagStr) : '' */
  return Cookies.get(AccountKey)
}

export function setAccount(userAccount) {
  Cookies.set('flag2', loginAuthorization)// 存门户编码加密后的字符串
  if (!userAccount) return
  const userInfo = {
    account: userAccount
  }
  const user = encryption({
    data: userInfo,
    key: 'quanyou,qupcloud',
    param: ['account']
  })
  return Cookies.set(AccountKey, user.account)
  /* const flagStr = JSON.stringify({ flag1: user.account, flag2: 'qup' })
  return Cookies.set(AccountKey, flagStr) */
}

export function removeAccount() {
  Cookies.remove('flag2')
  return Cookies.remove(AccountKey)
}

// 存储登录用户信息
const userKey = 'qy_user'
export function getUserInfo() {
  return getStore({ name: userKey }) || ''
}

export function setUserInfo(content) {
  setStore({
    name: userKey,
    content
  })
}

export function removeUserInfo() {
  removeStore({ name: userKey }) || ''
}
